package castle.defense.project;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
/**
 * Basic "soft" button class that will be used for any button in game or in menu.
 * 
 * @author Daniel Wasserman
 *
 */
public class SoftButton extends Renderable
{
	public static final int buttonBackground = 14873101;
	private RectF region;
	private Runnable r;
	private Paint p;
	private String textOne,textTwo;
	private BitmapDrawable draw;
	
	/**
	 * @param region constructor that takes the region of the screen this SoftButton instance will be accountable for.
	 * 
	 */
	public SoftButton(RectF region)
	{
		this.region = region;
		System.out.printf("Soft button created in region %s!\n",region);
		p = new Paint();
		p.setColor(Color.WHITE);
		p.setTextSize(25);
		p.setTypeface(Typeface.DEFAULT_BOLD);
		p.setStyle(Style.STROKE);
		textOne = "";
		textTwo = "";
		r = null;
		this.setDrawable((BitmapDrawable)GraphicsEngine.getDrawable(SoftButton.buttonBackground));
	}
	
	/**
	 * @param x1 Left coordinate in meters.
	 * @param y1 Top coordinate in meters.
	 * @param x2 Right coordinate in meters.
	 * @param y2 Bottom coordinate in meters.
	 */
	public SoftButton(double x1, double y1, double x2, double y2) {
		this(PE.screenRegion(x1, y1, x2, y2));
	}
	
	/**
	 * @param text The main text this soft button should display.
	 * 
	 * This only matters if there is no picture to draw.
	 */
	public void setTextOne(String text)
	{
		this.textOne = text;
	}
	
	/**
	 * @param text Secondary text for this soft button.
	 * 
	 * This only matters if there is no picture to draw.
	 */
	public void setTextTwo(String text)
	{
		this.textTwo = text;
		p.setTextSize(15);
	}
	
	/**
	 * @param r The runnable action that should occur when this soft button is pressed.
	 * 
	 * Note: Soft buttons will also be able to notify objects they were pressed.
	 */
	public void setRunnable(Runnable r)
	{
		this.r = r;
	}
	
	/**
	 * @param color The color of this soft button
	 * 
	 * This only matters if there is no picture to draw.
	 */
	public void setColor(int color)
	{
		p.setColor(color);
	}
	
	/**
	 * @param d The Drawable image that should be painted where this soft button is.
	 */
	public void setDrawable(BitmapDrawable d)
	{
		
		draw = new BitmapDrawable(d.getBitmap());
		draw.setBounds((int)(Math.round(region.left)),(int)(Math.round(region.top)),(int)(Math.round(region.right)),(int)(Math.round(region.bottom)));
	}
	
	/**
	 * @param c The canvas that this button should draw itself on.
	 */
	public void draw(Canvas c)
	{
		if(textTwo.equals(""))
		{
			draw.draw(c);
			float centerFix = p.measureText(textOne);
			c.drawText(textOne, (this.region.left + this.region.right - centerFix)/2, (this.region.top + this.region.bottom)/2, p);
		}
		else
		{
			float centerFix = p.measureText(textOne);
			c.drawText(textOne, (this.region.left + this.region.right - centerFix)/2, (this.region.top + this.region.bottom)/2-5, p);
			centerFix = p.measureText(textTwo);
			c.drawText(textTwo, (this.region.left + this.region.right - centerFix)/2, (this.region.top + this.region.bottom)/2+10, p);
		}
	}
	
	/**
	 * @param e The MotionEvent describing the tap on the screen.
	 * @return Returns true if this button was in the region, otherwise false.
	 */
	
	public boolean onTouch(MotionEvent e)
	{
		if(region.contains((int)e.getX(),(int)e.getY()))
		{
			if(r!=null)
				r.run();
			return true;
		}
		return false;
	}
	
	/**
	 * @param milli The time in milliseconds since the last update of this button.
	 * 
	 * For now, do nothing, later this can be used to make buttons GIF like or have effects on the inside.
	 */
	public void update(long milli) {
		//do nothing for now
	}
	
	/**
	 * @return The height on the screen the button should be considered. Returns 100 (Should be very high as they are to be directly accessible by the  user).
	 */
	public int height() {
		return 100;
	}
	
	/**
	 * The buttons should never be destroyed on the screen for now (may implement a change later where the soft button interface may be used to allow for checks of touches).
	 */
	public boolean destroy() {
		return false;
	}
}
